package com.example.wallpaperapp.m;

import android.util.Log;
import android.view.View;

import com.example.wallpaperapp.bean.UserInfo;
import com.example.wallpaperapp.ihttps.RetrofitService;
import com.example.wallpaperapp.ui.activity.SignInActivity;
import com.example.wallpaperapp.v.SininV;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * @Package: com.example.wallpaperapp.m
 * @ClassName: SiginModel
 * @Description: java类作用描述
 * @Author: zrmo
 * @CreateDate: 2020/11/13 21:24
 */

public class SiginModel implements SininM {
    SininV sininV = new SignInActivity();
    @Override
    public void postData(int userName, String password,callBack call) {
            Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("http://192.168.1.113:8080/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .addCallAdapterFactory(RxJava2CallAdapterFactory.create()) // 支持RxJava
                    .build();
            RetrofitService service = retrofit.create(RetrofitService.class);
            Observable<UserInfo> observable = service.addUser(userName, password);
            observable.subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(new Observer<UserInfo>() {
                        @Override
                        public void onSubscribe(Disposable d) {
                            Log.d("TAG", "开始采用subscribe连接");
                        }
                        @Override
                        public void onNext(UserInfo value) {
                            call.result(value);
                            call.onSuccess("成功");
                            Log.d("TAG", "onNext: "+value.toString() );
                        }
                        @Override
                        public void onError(Throwable e) {
                            call.onFailed(e.getMessage());
                            Log.d("TAG", "对Error事件作出响应"+e.getMessage());
                        }
                        @Override
                        public void onComplete() {
                            Log.d("TAG", "对Complete事件作出响应");
                        }
                    });


    }
}
